// =====================================================================
//
//  This file is part of the Microsoft Dynamics CRM SDK code samples.
//
//  Copyright (C) Microsoft Corporation.  All rights reserved.
//
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
// =====================================================================
namespace Microsoft.Xrm.Sdk.Query
{
    using System.Diagnostics.CodeAnalysis;
	using System.Runtime.Serialization;

	[SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix", Justification = "Suffix is correct")]
	[DataContract(Name = "QueryByAttribute", Namespace = XmlNamespaces.V5.Contracts)]
	public sealed class QueryByAttribute : QueryBase
	{
		public QueryByAttribute()
		{
			_columnSet = new ColumnSet();
			_pageInfo = new PagingInfo();
		}

		public QueryByAttribute(string entityName)
		{
			this.EntityName = entityName;
		}

		[DataMember]
		public string EntityName
		{
			get
			{
				return _entityName;
			}
			set
			{
				_entityName = value;
			}
		}

		[DataMember]
		public DataCollection<string> Attributes
		{
			get
			{
				if (_attributes == null)
				{
					_attributes = new DataCollection<string>();
				}
				return _attributes;
			}
			private set
			{
				_attributes = value;
			}
		}

		[DataMember]
		public DataCollection<object> Values
		{
			get
			{
				if (_attributeValues == null)
				{
					_attributeValues = new DataCollection<object>();
				}
				return _attributeValues;
			}
			private set
			{
				_attributeValues = value;
			}
		}

		[DataMember]
		public PagingInfo PageInfo
		{
			get
			{
				return _pageInfo;
			}
			set
			{
				_pageInfo = value;
			}
		}

		[DataMember]
		public ColumnSet ColumnSet
		{
			get
			{
				return _columnSet;
			}
			set
			{
				_columnSet = value;
			}
		}

		[DataMember]
		public DataCollection<OrderExpression> Orders
		{
			get
			{
				if (_orders == null)
				{
					_orders = new DataCollection<OrderExpression>();
				}
				return _orders;
			}
			private set
			{
				_orders = value;
			}
		}

		[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
		internal override void Accept(IQueryVisitor visitor)
		{
			visitor.Visit(this);
		}

		public void AddOrder(string attributeName, OrderType orderType)
		{
			this.Orders.Add(new OrderExpression(attributeName, orderType));
		}

		public void AddAttributeValue(string attributeName, object value)
		{
			this.Attributes.Add(attributeName);
			this.Values.Add(value);
		}

		private string _entityName;
		private DataCollection<string> _attributes;
		private DataCollection<object> _attributeValues;
		private PagingInfo _pageInfo;
		private ColumnSet _columnSet;
		private DataCollection<OrderExpression> _orders;
	}
}

